package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.StrategyPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.StrategyBean;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.zhouyou.view.seekbar.SignSeekBar;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.FUNCTIONS_STRATEGY)
public class StrategyActivity extends BaseActivity<StrategyPresenter> {


    @BindView(R2.id.strategy_xianshi_iv)
    ImageView strategyXianshiIv;
    @BindView(R2.id.strategy_cny_iv)
    TextView strategyCnyIv;
    @BindView(R2.id.strategy_sysy_iv)
    TextView strategySysyIv;
    @BindView(R2.id.strategy_ljsy_iv)
    TextView strategyLjsyIv;
    @BindView(R2.id.strategy_record_iv)
    LinearLayout strategyRecordIv;
    @BindView(R2.id.strategy_rv)
    RecyclerView strategyRv;
    @BindView(R2.id.strategy_rv_two)
    RecyclerView strategyRvTwo;
    @BindView(R2.id.strategy_find_all)
    LinearLayout strategyFindAll;
    @BindView(R2.id.strategy_xiangxia_iv)
    ImageView strategyXiangxiaIv;
    @BindView(R2.id.strategy_zonge_iv)
    TextView strategyZongeIv;
    @BindView(R2.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private boolean isShow = false;
    private BaseAdapter<StrategyBean.List1Bean> adapter;
    private BaseAdapter<StrategyBean.List2Bean> myAdapter;
    private StrategyBean data;
    private String pay_type = "usdt";
    private int page = 1;
    private String state = "1";
    private double progress;
    private boolean status = true;
    private String languages = "1";

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_strategy;
    }

    @Override
    public StrategyPresenter getPresenter() {
        return new StrategyPresenter();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        if (Constans.LANGUAGE_EN.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "2";
        } else if (Constans.LANGUAGE_CNA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "1";
        } else if (Constans.LANGUAGE_TRA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "3";
        }
        setSize();
        setData();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initData() {
        //获取策略投资数据
        mPresenter.getStrategyData(pay_type, "1", page + "", languages, true);

        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getStrategyData(pay_type, "1", page + "", languages, false);
        });
        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getStrategyData(pay_type, "1", page + "", languages, false);
        });

        ClickUtil.click(300, strategyRecordIv, () -> {
            ARouter.getInstance().build(RConfig.FUNCTIONS_ROLL_RECORD).withString(Constans.TYPE, Constans.CLTZ).navigation();
        });

        ClickUtil.click(300, strategyXianshiIv, () -> {
            if (isShow) {
                if (data != null) {
                    strategyZongeIv.setText(CommonUtil.dealCoinNum(data.getPrice(), Constans.COIN_USDT));
                    strategySysyIv.setText(NumberUtil.keep2(data.getProfit()));
                    strategyLjsyIv.setText(NumberUtil.keep2(data.getCumulative()));
                    strategyCnyIv.setText("≈" + NumberUtil.keep2(ArithUtil.mul(Double.valueOf(data.getPrice()), data.getExchange_rate())) + " CNY");
                    strategyXianshiIv.setImageResource(R.mipmap.lib_see);
                    isShow = false;
                } else {
                    strategyZongeIv.setText("0.00");
                    strategySysyIv.setText("0.00");
                    strategyLjsyIv.setText("0.00");
                    strategyCnyIv.setText("≈" + "0.00" + " CNY");
                    strategyXianshiIv.setImageResource(R.mipmap.lib_see);
                    isShow = false;
                }
            } else {
                strategyZongeIv.setText("******");
                strategyCnyIv.setText("≈ ****** CNY");
                strategySysyIv.setText("******");
                strategyLjsyIv.setText("******");
                strategyXianshiIv.setImageResource(R.mipmap.lib_unsee);
                isShow = true;
            }
        });
        ClickUtil.click(300, strategyFindAll, () -> {
            if (status) {
                status = false;
                strategyRvTwo.setVisibility(View.VISIBLE);
                strategyXiangxiaIv.setVisibility(View.GONE);
            } else {
                status = true;
                strategyRvTwo.setVisibility(View.GONE);
                strategyXiangxiaIv.setVisibility(View.VISIBLE);
            }
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, StrategyActivity.class);
        context.startActivity(intent);
    }

    @SuppressLint("CheckResult")
    private void setSize() {
        strategyRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<StrategyBean.List1Bean>(R.layout.strategy_item_layout, null, strategyRv, true) {
            @Override
            public void bind(ViewHolder holder, StrategyBean.List1Bean item) {
                state = item.getStatus();
//                holder.setText(R.id.tv_name, item.getDescribe());
                SignSeekBar seekBar = holder.getView(R.id.seekbar);
                seekBar.setEnabled(false);
                ImageView iv = holder.getView(R.id.yigaoqing);
                LinearLayout ll = holder.getView(R.id.strategy_item_ll);
                try {
                    double div = ArithUtil.div(Double.parseDouble(item.getSurplus()), Double.parseDouble(item.getTotal_price()), 2);
                    progress = ArithUtil.sub(100, ArithUtil.mul(div, 100));
                    seekBar.getConfigBuilder().floatType().min(0).max(100).build();
                    //进图条进度
                    seekBar.setProgress((int) progress);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

                if (state.equals("1")) {
                    iv.setVisibility(View.GONE);
                    holder.setText(R.id.title, item.getName())
                            .setText(R.id.state, App.INSTANCE.getString(R.string.functions_strategyActivity1))
                            .setText(R.id.nianhualv, NumberUtil.keep2(item.getInterest_rate()) + "%")
                            .setText(R.id.shengyuketou, App.INSTANCE.getString(R.string.functions_strategyActivity2) + CommonUtil.dealCoinNum(item.getSurplus(), item.getPay_type().toUpperCase()) + item.getPay_type().toUpperCase())
                            .setText(R.id.time, item.getCycle() + App.INSTANCE.getString(R.string.functions_strategyActivity3))
                            .setText(R.id.strategy_progress_tv, progress + "%")
                            .setText(R.id.zonge, App.INSTANCE.getString(R.string.functions_strategyActivity4) + CommonUtil.dealCoinNum(item.getTotal_price(), item.getPay_type().toUpperCase()) + item.getPay_type().toUpperCase());
                } else {
                    return;
                }
                ClickUtil.click(300, ll, () -> {
                    ARouter.getInstance().build(RConfig.FUNCTIONS_DETAILS)
                            .withString(Constans.TYPE, Constans.FUNCTIONS_CLTZ)
                            .withSerializable("List1Bean", item)
                            .withInt("itemType", 0)
                            .navigation();
                });
            }
        };
    }

    @SuppressLint("CheckResult")
    private void setData() {
        strategyRvTwo.setLayoutManager(new LinearLayoutManager(this));
        myAdapter = new BaseAdapter<StrategyBean.List2Bean>(R.layout.strategy_item_layout, null, strategyRvTwo, true) {
            @Override
            public void bind(ViewHolder holder, StrategyBean.List2Bean item) {
                state = item.getStatus();
//                holder.setText(R.id.tv_name, item.getDescribe());
                SignSeekBar seekBar = holder.getView(R.id.seekbar);
                seekBar.setEnabled(false);
                state = item.getStatus();
                //进度条
                seekBar.getConfigBuilder().floatType().min(0).max(100).build();
                //进图条进度
                seekBar.setProgress(100);
                try {
                    double div = ArithUtil.div(Double.parseDouble(item.getSurplus()), Double.parseDouble(item.getTotal_price()), 2);
                    progress = ArithUtil.sub(100, ArithUtil.mul(div, 100));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                if (!state.equals("1")) {
                    holder.setText(R.id.title, item.getName())
                            .setText(R.id.state, "")
                            .setText(R.id.nianhualv, NumberUtil.keep2(item.getInterest_rate()) + "%")
                            .setText(R.id.shengyuketou, App.INSTANCE.getString(R.string.functions_strategyActivity2) + CommonUtil.dealCoinNum(item.getSurplus(), item.getPay_type().toUpperCase()) + item.getPay_type().toUpperCase())
                            .setText(R.id.time, item.getCycle() + App.INSTANCE.getString(R.string.functions_strategyActivity3))
                            .setText(R.id.strategy_progress_tv, "100%")
                            .setText(R.id.zonge, App.INSTANCE.getString(R.string.functions_strategyActivity4) + CommonUtil.dealCoinNum(item.getTotal_price(), item.getPay_type().toUpperCase()) + item.getPay_type().toUpperCase());
//                    //进度条
//                    seekBar.getConfigBuilder().floatType().min(0).max(100).build();
//                    //进图条进度
//                    seekBar.setProgress(100);
                } else {
                    return;
                }
                LinearLayout llItem = holder.getView(R.id.strategy_item_ll);
                ClickUtil.click(300, llItem, () -> {
                    ARouter.getInstance().build(RConfig.FUNCTIONS_DETAILS)
                            .withString(Constans.TYPE, Constans.FUNCTIONS_CLTZ)
                            .withSerializable("List2Bean", item)
                            .withInt("itemType", 1)
                            .navigation();
                });
            }
        };
    }

    @SuppressLint("SetTextI18n")
    public void getStrategyDataSuccess(StrategyBean data) {
        this.data = data;
        if (data != null) {
            strategyZongeIv.setText(CommonUtil.dealCoinNum(data.getPrice(), Constans.COIN_USDT));
            strategySysyIv.setText(NumberUtil.keep2(data.getProfit()));
            strategyLjsyIv.setText(NumberUtil.keep2(data.getCumulative()));
            strategyCnyIv.setText("≈" + NumberUtil.keep2(ArithUtil.mul(Double.valueOf(data.getPrice()), data.getExchange_rate())) + " CNY");
            if (adapter != null) {
                if (page == 1) {
                    adapter.setNewData(data.getList1());
                } else {
                    adapter.addData(data.getList1());
                }
            }
            if (myAdapter != null) {
                if (page == 1) {
                    myAdapter.setNewData(data.getList2());
                } else {
                    myAdapter.addData(data.getList2());
                }
            }
        }
    }

    public void onStopRefresh() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.finishLoadMore();
        }
    }
}
